﻿/**
* Urn.js
* 
* @author Demis Bellot
* @version 1.0
*
* Copyright 2009, Demis Bellot
* http://code.google.com/p/ajaxstack/
*/

function Urn(resource, name, id)
{
	this.resource = resource;
	this.name = id ? name : null;
	this.id = id ? id : name;
}
Urn.log = new Logger("Urn");
p = Urn.prototype;
p.toUrn = function()
{
	return this.name
		? "urn:" + this.resource + ":" + this.name + ":" + this.id
		: "urn:" + this.resource + ":" + this.id;
};
p.getResource = function()
{
	return this.resource;
};
p.getName = function()
{
	return this.name;
};
p.getId = function()
{
	return this.id;
};
Urn.parse = function(urnStr)
{
	if (!urnStr) return null;

	var parts = urnStr.split(":");

	if (parts.length < 3 || parts.length > 4)
	{		
		Urn.log.error("Invalid Urn: " + urnStr);
		return null;
	}

	return parts.length > 2
		? new Urn(parts[1], parts[2], parts[3])
		: new Urn(parts[1], parts[2]);
};
Urn.getResource = function(urn)
{
	var urn = Urn.parse(urn);
	return urn ? urn.resource : null;
};
Urn.getName = function(urn)
{
	var urn = Urn.parse(urn);
	return urn ? urn.name : null;
};
Urn.getId = function(urn)
{
	var urn = Urn.parse(urn);
	return urn ? urn.id : null;
};
Urn.getIdNumber = function(urn)
{
	return parseInt(Urn.getId(urn));
};
